package com.fenghuajueli.lib_pictureselect.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ConvertUtils;
import com.fenghuajueli.lib_pictureselect.R;
import com.fenghuajueli.lib_pictureselect.activity.PictureSelectActivity;
import com.fenghuajueli.lib_pictureselect.adapter.PictureSelectItemAdapter;
import com.fenghuajueli.lib_pictureselect.entity.SelectMediaEntity;
import com.fenghuajueli.lib_pictureselect.interfaces.OnListItemListener;
import com.fenghuajueli.lib_pictureselect.utils.GridSpacingItemDecoration;
import com.fenghuajueli.lib_pictureselect.utils.MediaSelectDataUtils;

/**
 * 选择视频界面
 */
public class SelectVideoFragment extends Fragment {

    private RecyclerView recyclerView;
    private View contentView;
    private PictureSelectItemAdapter pictureSelectItemAdapter;
    private GridLayoutManager gridLayoutManager;


    public static SelectVideoFragment newInstance() {
        SelectVideoFragment fragment = new SelectVideoFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.fragment_select_video, container, false);
        recyclerView = contentView.findViewById(R.id.recyclerView);
        pictureSelectItemAdapter = new PictureSelectItemAdapter(getActivity(), MediaSelectDataUtils.videoSelectMediaEntityList);
        pictureSelectItemAdapter.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((PictureSelectActivity) getActivity()).refreshPhotoPosition();
            }
        });
        pictureSelectItemAdapter.setPreviewListener(new OnListItemListener<SelectMediaEntity>() {
            @Override
            public void onItemClick(int position, SelectMediaEntity data) {
                ((PictureSelectActivity) getActivity()).goPreview(1, position);
            }
        });
        gridLayoutManager = new GridLayoutManager(getActivity(), 3);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(3, ConvertUtils.dp2px(14), true));
        recyclerView.setAdapter(pictureSelectItemAdapter);
        return contentView;
    }

    public void refreshPosition() {
        if (pictureSelectItemAdapter != null) {
            pictureSelectItemAdapter.notifyDataSetChanged();
        }
    }

}